<?php
/**
 * Citation
 *
 * Description de la classe
 *
 * @author Mathieu
 */
class Quote extends Object {
    protected $id;        // Identifiant de la citation
    protected $nom;       //citation
    protected $slug;      //slug de la citation

    function __construct($id="", $params=array()) {
        if($id != "") {
            $this->id = $id;
            foreach($params as $key => $p) {
                $this->$key = $p;
            }
        }
    }

    public function load() {
        $db = Database::getInstance();
        $result = $db->query("SELECT * FROM citations WHERE id = '".$this->id."'");
        if(sizeof($result) > 0) {
            foreach($result[0] as $key => $p) {
                $this->$key = $p;
            }
            return;
        }
        if(DEBUG) echo("Objet inconnu");
        return false;
    }

    public function save() {
        $db = Database::getInstance();
        $nav = Navigation::getInstance();
        $this->slug = $nav->getRewrite($this->nom);
        if(isset($this->id)) {

        } else {
            $sql = "INSERT INTO citations VALUES('', '".addslashes($this->nom)."', '".$this->slug."', '', '', '')";
        }
        return $db->query($sql);
    }

    function __toString() {
        if(!isset($this->nom)) $this->load();
        return $this->nom;
    }

    static function getRandomQuotes($max='0'){
        $db = Database::getInstance();
        $sql = "SELECT id, nom, slug FROM citations ORDER BY RAND()";
        if($max!='0')  $sql.=" LIMIT ".$max;
        $result = $db->query($sql);
        foreach($result as $quote) {
                $quotes[] = new Quote($quote['id'], array('nom'=>$quote['nom'],
                                                          'slug'=>$quote['slug']
                                                        ));
        }
        return $quotes;

    }
}
?>
